"""
@author: chy
@file: atm机.py
@time: 2023/12/5 12:52
"""
import json

pwd_dic = {}   #{"root":"123456"}
cash_dic = {}  #{"root":1000 }
#{"root":{"password":"123456", "balance":1000},
# "admin:{"password":"admin123", "balance":5000}}
def login(name,passwd):
    if name in pwd_dic  and pwd_dic[name] == passwd:
        return True

def cashinput(name,cash):

    # if name not in cash_dic  :
    #     cash_dic[name] = cash
    # else:
    #     cash_dic[name] += cash
    cash_dic[name] = cash_dic.get(name,0) + cash
    print(f"存钱成功！！！\n您现在的余额为：{cash_dic[name]}元人民币")
def cashoutput(name,cash):
    exsitc = cash_dic.get(name,0)
    if exsitc >= cash:
        cash_dic[name] = exsitc - cash
        print(f"取钱成功！！！\n您现在的余额为：{cash_dic[name]}元人民币")
    else:
        print("您的余额已不足，请及时充值...")

def transusr(name,cash,trausr):
    exsitc1 = cash_dic.get(name,0)
    exsitc2 = cash_dic.get(trausr,0)
    if exsitc1 >= cash :
        if trausr in cash_dic:
            cash_dic[name] = exsitc1 - cash
            cash_dic[trausr] = exsitc2 + cash
            print(f"转账成功！！！\n您现在的余额为：{cash_dic[name]}元人民币")
            print(f"他现在的余额为：{cash_dic[trausr]}元人民币")
        else:
            cash_dic[name] = exsitc1 - cash
            cash_dic[trausr] = cash
            print(f"转账成功！！！\n您现在的余额为：{cash_dic[name]}元人民币")
            print(f"他现在的余额为：{cash_dic[trausr]}元人民币")
    else:
        print("您的余额已不足，请及时充值后再转账...")
def register(name,passwd):
    if name not in pwd_dic:
        pwd_dic[name] = passwd
        print(f"当前用户有{pwd_dic}")
        return True
    else:
        print("很抱歉，该用户已存在，请重新更换用户名完成注册...")
        return False

while 1:
    choice = input("欢迎来到陈氏atm机：\n1.登录\n2.注册\n3.退出\n请输入您需要进行的操作：")
    if choice == "1":
        usrname = input("请输入您的用户名，输入q退出:")
        if usrname == "q":
            continue
        passwrd = input("请输入您的密码:")
        if login(usrname,passwrd):
            print("登陆成功！！！")
            while 1:
                selct = input("请选择您要进行的操作：\na存钱\nb取钱\nc转账\nd退出\n")
                if selct == "a":
                    cashin = int(input(f"您现在的余额为：{cash_dic.get(usrname,0)}元人民币请输入您需要存入的金额："))
                    cashinput(usrname,cashin)
                elif selct == "b":
                    cashout = int(input(f"您现在的余额为：{cash_dic.get(usrname,0)}元人民币,请输入您需要取走的金额："))
                    cashoutput(usrname,cashout)
                elif selct == "c":
                    print("请在以下银行现有用户中选择您要转账的对象：")
                    for key in pwd_dic:
                        print(key, end=" ")
                    transuser = input("\n请选择您需要转账的用户：")
                    if transuser not in pwd_dic:
                        print("当前用户不存在，请勿轻信他人，以防电信诈骗！")
                    elif transuser != usrname:
                        cashtrans = int(input(f"您现在的余额为：{cash_dic.get(usrname,0)}元人民币,请输入您需要转账的金额："))
                        transusr(usrname,cashtrans,transuser)
                    else:
                        print("很抱歉您没必要给自己转账")
                elif selct == "d":
                    break
                else:
                    print("请重新输入正确操作选项..：")

        else:
            print("输入用户名或密码有误，登陆失败...")

    elif choice == "2":
        print("请设置您的用户名与密码,输入q退出：")
        usrname = input("请设置您的用户名:")
        if usrname == "q":
            continue
        passwrd = input("请设置您的密码:")
        if register(usrname,passwrd):
            print("注册成功!!!")
        else:
            print("注册失败...")
    elif choice == "3":
        break
    else:
        print("请重新输入正确选项...：")
    with open('用户密码表.text','w') as f1:
        json.dump(pwd_dic,f1)
    with open('用户余额表.text','w') as f2:
        json.dump(cash_dic,f2)
